package com.carleasoft.mps.cms.business.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.annotation.OpImgAnnotation;
import com.carleasoft.mps.cms.business.article.bean.entity.AuthorityArticleEntity;
import com.carleasoft.mps.cms.business.article.bean.po.ArticlePagePo;
import com.carleasoft.mps.cms.business.article.bean.po.ArticlePo;
import com.carleasoft.mps.cms.business.article.bean.po.ArticleStatusPo;
import com.carleasoft.mps.cms.business.article.bean.po.CollectorPo;
import com.carleasoft.mps.cms.business.article.bean.vo.ArticlePageVo;
import com.carleasoft.mps.cms.business.article.bean.vo.ArticleVo;
import com.carleasoft.mps.cms.business.article.service.ArticleService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Description: BackstageArticleController</p>
 *
 * @author
 * @since 2021/7/5
 */
@RestController
@RequestMapping("/backstage")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 获取信息列表,分页
     *
     * @param articlePagePo
     * @return ResultBody
     */
    @PostMapping("/pageQuery")
    @OpImgAnnotation("imagePath")
    public R<Page<ArticlePageVo>> pageQuery(@RequestBody ArticlePagePo articlePagePo, HttpServletRequest request) {

        Page<ArticlePageVo> page = articleService.pageQuery(new Page(articlePagePo.getCurrent(), articlePagePo.getSize()), articlePagePo);
        return R.ok(page);
    }


    /**
     * 文章新增
     *
     * @param articlePo
     * @param
     * @return
     */
    @PostMapping("/insertArticle")
    public R insertArticle(@RequestBody ArticlePo articlePo) {

        return result(articleService.insertArticle(articlePo));
    }

    /**
     * 详情查询
     *
     * @param articleStatusPo
     * @return
     */
    @PostMapping("/backstageArticleDetail")
    @OpImgAnnotation("imagePath")
    public R findByIdArticle(@RequestBody ArticleStatusPo articleStatusPo) {
        ArticleVo articleVo = articleService.findByIdArticle(articleStatusPo.getArticleCode(), null);
        return R.ok(articleVo);
    }

    /**
     * 状态修改
     */
    @PostMapping("/update/articleStatus")
    public R updateArticleStatus(@RequestBody ArticleStatusPo articleStatusPo) {

        return result(articleService.updateArticleStatus(articleStatusPo));
    }


    /**
     * 文章内容修改
     *
     * @param articlePo
     * @param request
     * @return
     */
    @PostMapping("/update/article")
    public R updateArticle(@RequestBody ArticlePo articlePo, HttpServletRequest request) {

        return result(articleService.updateArticle(articlePo));
    }

    @PutMapping("/update/weight")
    public R updateWeight(@RequestBody AuthorityArticleEntity authorityArticleEntity) {
        return result(articleService.updateWeight(authorityArticleEntity));
    }

    /**
     * 置顶
     *
     * @param authorityArticleEntity
     * @return
     */
    @PostMapping("/update/top")
    public R top(@RequestBody AuthorityArticleEntity authorityArticleEntity) {
        return result(articleService.toTop(authorityArticleEntity));
    }


    /**
     * 文章-收藏/取消收藏
     * @param collectorPo
     * @return
     */
    @PostMapping("/collector")
    public R collector(@RequestBody CollectorPo collectorPo) {
        boolean isOk = articleService.collector(collectorPo);
        return R.ok(isOk);
    }


    public R result(int result) {
        if (result > 0) {
            return R.ok();
        } else {
            return R.failed();
        }
    }
}
